﻿//========================================================= 
/**@file Mifare1Card_TestCase.h
 * @brief M1卡测试案例
 * 
 * @date 2016-08-13   15:14:09
 * @author Zhyioo 
 * @version 1.0
 */ 
//--------------------------------------------------------- 
#ifndef _LIBZHOUYB_MIFARE1CARD_TESTCASE_H_
#define _LIBZHOUYB_MIFARE1CARD_TESTCASE_H_
//--------------------------------------------------------- 
#include "../TestFrame.h"

#include "../../../device/iccard/extension/base/IMemoryCard.h"
using zhou_yb::device::iccard::extension::base::IMifare1Card;
//--------------------------------------------------------- 
namespace zhou_yb {
namespace application {
namespace test {
//--------------------------------------------------------- 
/// M1卡测试案例
struct Mifare1Card_TestCase : public ITestCase<IMifare1Card>
{
    /// 测试对扇区数据的读写
    virtual bool Test(Ref<IMifare1Card>& testObj, const ByteArray& testArg, TextPrinter& printer)
    {
        printer.TextPrint(TextPrinter::TextNote, "M1卡测试");
        // 解析数据
        ByteBuilder pwd(8);
        byte sectorIndex = 0;
        byte blockIndex = 0;
        char keyType = 'A';

        StringArguments arg;
        ArgParser parser;
        if(!parser.Parse(testArg, arg))
        {
            DevCommand::FromAscii(testArg, pwd);
        }
        else
        {
            sectorIndex = _itobyte(arg["Sector"].To<size_t>(0));
            blockIndex = _itobyte(arg["Block"].To<size_t>(0));
            keyType = arg["KeyType"].To<char>('A');
            
            if(!arg["Key"].Get<ByteBuilder>(pwd))
            {
                // 设置默认密钥
                pwd.Append(0xFF, 6);
            }
        }
        if(!testObj->VerifySector(sectorIndex, pwd, keyType))
        {
            printer.TextPrint(TextPrinter::TextError, "扇区认证失败");
            return false;
        }

        ByteBuilder random(16);
        ByteConvert::Random(random, 16);

        if(!testObj->WriteBlock(blockIndex, random))
        {
            printer.TextPrint(TextPrinter::TextError, "写块数据失败");
            return false;
        }

        ByteBuilder tmp(16);
        if(!testObj->ReadBlock(blockIndex, tmp))
        {
            printer.TextPrint(TextPrinter::TextError, "块读数据失败");
            return false;
        }

        if(!random.IsEqual(tmp))
        {
            printer.TextPrint(TextPrinter::TextError, "比对写入数据失败");
            return false;
        }
        return true;
    }
};
//--------------------------------------------------------- 
} // namespace test 
} // namespace application 
} // namespace zhou_yb 
//--------------------------------------------------------- 
#endif // _LIBZHOUYB_MIFARE1CARD_TESTCASE_H_
//========================================================= 